﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Avalon.Core.MVC.StateBags {
    public class ContextEntityBag<TViewModel, TModel> : IContextEntityBag<TViewModel, TModel> {
        public Dictionary<string, object> ParameterValues { get; set; }
        private Exception _error;
        public Exception Error {
            get { return _error; }
            set {
                _error = value;
            }
        }

        public IEnumerable<TViewModel> ViewModels { get; set; }
        public IQueryable<TModel> Models { get; set; }
        public TViewModel ViewModel { get; set; }
        public TModel Model { get; set; }

        public BagState BagState { get; set; }

        public dynamic Result {
            get {
                if (this.ViewModels != null) return this.ViewModels;
                if (this.ViewModel != null) return this.ViewModel;

                return ViewModels;
            }
            set {
                this.ViewModels = value;
            }
        }


        public ContextEntityBag() {
            this.ParameterValues = new Dictionary<string, object>();
        }
        public ContextEntityBag(IEnumerable<TViewModel> viewModels)
            : this() {
            this.ViewModels = viewModels;
        }
        public ContextEntityBag(TViewModel viewModel)
            : this() {
            this.ViewModel = viewModel;
        }
    }
}
